﻿using MediatR;
using Traceable.App.Application.Command.Aquaculture;
using Traceable.Domian;
using Traceable.ErrorCode;
using Traceable.Infastructure;

namespace Traceable.App.Application.Handler.Aquaculture
{
    public class DeleteImmunityHandler : IRequestHandler<DeleteImmunityCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Immunity_Table> _immunityRepo;

        public DeleteImmunityHandler(IBaseRepository<Immunity_Table> immunityRepo)
        {
            _immunityRepo = immunityRepo;
        }
        /// <summary>
        /// 免疫信息删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(DeleteImmunityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var immunity = _immunityRepo.GetAll().Where(p => p.Id == request.Id && !p.IsDelete).FirstOrDefault();
            if (immunity != null)
            {
                immunity.IsDelete = true;
            }
            var res = _immunityRepo.Update(immunity);
            if (res > 0)
            {
                result.Code = StatusEnum.OK;
                result.Data = res;
                result.Message = "删除成功";
            }
            else
            {
                result.Code = StatusEnum.Fail;
                result.Data = res;
                result.Message = "删除失败";
            }
            return Task.FromResult(result);
        }
    }
}
